package knowse.app.daos;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.os.Parcel;
import android.os.Parcelable;


public class CommitDataObject implements Parcelable{

	static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm.ss");
	private String commitID;
	private String jsonClass;
	private Date timeStamp;
	private String startDate;
	private String startTime;
	private String commitHash;
	private String authorName;
	private String shortcommitHash;
	List<ChangeDataObject> changes;
	//In JSONfile they use StartDate and StartTime.
	
	
	public CommitDataObject(JSONObject commit) throws Exception{
		this.jsonClass=commit.optString("jsonClass", "null");
		this.commitID=commit.optString("commitID", "null");
		String startDate=commit.optString("StartDate", "null");
		String startTime=commit.optString("StartTime", "null");
		this.timeStamp = df.parse(startDate + " " + startTime);
		this.commitHash=commit.optString("commitHash", "null");
		this.authorName=commit.optString("authorName", "null");
		this.shortcommitHash=commit.optString("shortcommitHash", "null");
		JSONArray jsonChanges=commit.getJSONArray("Changes");
		for(int i=0;i<jsonChanges.length();i++)
		{
			changes.add(new ChangeDataObject(jsonChanges.getJSONObject(i)));
		}

		
	}

	public CommitDataObject(String jsonClass, String commitID, Date timeStamp,String startDate, String startTime, String commitHash,String shortcommitHash, String authorName, List<ChangeDataObject> changes) {
		this.jsonClass = jsonClass;
		this.commitID = commitID;
		this.timeStamp=timeStamp;
		this.startDate=startDate;
		this.startTime=startTime;
		this.commitHash = commitHash;
		this.shortcommitHash = shortcommitHash;
		this.authorName = authorName;
		this.changes = changes;
	}

	public CommitDataObject(){
		
	}

	public String getJsonClass() {
		return jsonClass;
	}

	public void setJsonClass(String jsonClass) {
		this.jsonClass = jsonClass;
	}
	public String getCommitID() {
		return commitID;
	}

	public void setCommitID(String commitID) {
		this.commitID = commitID;
	}
	
	public Date getTimeStamp() {
		return timeStamp;
	}

	public void setTimeStamp(Date timeStamp) {
		this.timeStamp = timeStamp;
	}

	public String getStartDate(){
		
		return startDate;
	}
	public void setStartDate(String startDate){
		this.startDate= startDate;
	}
	public String getStartTime(){
		
		return startTime;
	}
	public void setStartTime(String startTime){
		this.startTime= startTime;
	}
	public String getCommitHash() {
		return commitHash;
	}
	
	public void setCommitHash(String commitHash) {
		this.commitHash = commitHash;
	}

	public String getShortCommitHash() {
		return shortcommitHash;
	}
	
	public void setShortCommitHash(String shortcommitHash) {
		this.shortcommitHash = shortcommitHash;
	}
	public String getAuthorName() {
		return authorName;
	}

	public void setAuthorName(String authorName) {
		this.authorName = authorName;
	}

	public List<ChangeDataObject> getChanges() {
		return changes;
	}

	public void setChanges(List<ChangeDataObject> changes) {
		this.changes = changes;
	}

	@Override
	public String toString() {
		return "json_class: " + jsonClass + ", timeStamp: "
				+ df.format(timeStamp) + ", authorName: "
				+ authorName;
	}

	public int describeContents() {
		return 0;
	}

	public void writeToParcel(Parcel parcel, int flags) {
		parcel.writeString(jsonClass);
		parcel.writeString(commitID);
		parcel.writeString(df.format(timeStamp));
		parcel.writeString(commitHash);
		parcel.writeString(shortcommitHash);
		parcel.writeString(authorName);
		parcel.writeList(changes);	
	}
	
	public static final Parcelable.Creator<CommitDataObject> CREATOR= new Creator<CommitDataObject>(){

		public CommitDataObject createFromParcel(Parcel source) {
			CommitDataObject commitDataObj=new CommitDataObject();
			commitDataObj.jsonClass=source.readString();
			try {
				commitDataObj.timeStamp = df.parse(source.readString());
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			commitDataObj.commitID=source.readString();
			commitDataObj.commitHash=source.readString();
			commitDataObj.shortcommitHash=source.readString();
			commitDataObj.authorName=source.readString();
			//TODO commitDataObj.changes=source.readArrayList(outVal, loader);
			return commitDataObj;
		}

		public CommitDataObject[] newArray(int size) {
			return new CommitDataObject[size];
		}};

}